package com.example.androidpj;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.List;



public class learnActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);

        setContentView(R.layout.activity_learn);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        EditText userName = findViewById(R.id.userName);
        EditText password = findViewById(R.id.password);
        Button btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userNameStr = userName.getText().toString();
                String passwordStr = password.getText().toString();
                if (userNameStr.isEmpty() || passwordStr.isEmpty() ) {
                    Toast.makeText(learnActivity.this, "请输入用户名或密码", Toast.LENGTH_SHORT).show();
                } else if (userNameStr.equals("12345") && passwordStr.equals("12345")){
                    Toast.makeText(learnActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(learnActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

}



class MyArrayAdapter extends ArrayAdapter<Spitem> {

    //    List<Spitem> spItemsList = new ArrayList<>();
//        Spinner spinner = findViewById(R.id.sp);
//        Spitem item1 = new Spitem("1", R.drawable.gopher);
//        Spitem item2 = new Spitem("2", R.drawable.gopher);
//        Spitem item3 = new Spitem("3", R.drawable.gopher);
//        Spitem item4 = new Spitem("4", R.drawable.gopher);
//        spItemsList.add(item1);
//        spItemsList.add(item2);
//        spItemsList.add(item3);
//        spItemsList.add(item4);
//        MyArrayAdapter adapter = new MyArrayAdapter(this,
//                R.layout.sp_item, spItemsList);
//        spinner.setAdapter(adapter);
    private List<Spitem> spItemsList;

    private int resourceId;

    private Context context;

    /**
     * @param context     上下文对象
     * @param resource    布局资源id
     * @param spItemsList 需要渲染的数据
     */
    public MyArrayAdapter(@NonNull Context context,
                          int resource, List<Spitem> spItemsList) {
        super(context, resource, spItemsList);
        this.spItemsList = spItemsList;
        this.resourceId = resource;
        this.context = context;
    }

    /**
     * 为了渲染下拉的信息
     *
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return getView(position, convertView, parent);
    }

    /**
     * @param position    当前是第position条数据
     * @param convertView 当前的视图
     * @param parent
     * @return
     */
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            // 绑定布局
            convertView = LayoutInflater.from(context)
                    .inflate(resourceId, parent, false);
        }
        // 获取第i项的视图
        View view = convertView;
        // 获取资源控件
        TextView textView = view.findViewById(R.id.txt);
        ImageView imageView = view.findViewById(R.id.arrow);
        // 设置textview的文本
        textView.setText(spItemsList.get(position).getName());
        // 设置img的资源id
        imageView.setImageResource(spItemsList.get(position).getImgId());
        return view;
    }
}

